package leetcode;

public class Leetcode198 {

    public static int rob1(int[] nums) {
        if (nums.length == 1) {
            return nums[0];
        }
        if(nums.length == 2) {
            return Math.max(nums[0], nums[1]);
        }

        int[] maxMoney = new int[nums.length];
        maxMoney[0] = nums[0];
        maxMoney[1] = nums[1];

        for (int i = 2;i < nums.length;i++) {
            maxMoney[i] = Math.max(maxMoney[i-1], maxMoney[i-2] + nums[i]);
        }
        return maxMoney[nums.length-1];
    }

    public static int rob2(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int len = nums.length;
        if (len == 1) {
            return nums[0];
        }
        int[] dp = new int[len];
        dp[0] = nums[0];
        dp[1] = Math.max(nums[0], nums[1]);
        for (int i = 2;i < len;i++) {
            dp[i] = Math.max(dp[i-1], dp[i-2] + nums[i]);
        }
        return dp[len-1];
    }


    public static int rob(int[] nums) {
        if (nums.length == 1) {
            return nums[0];
        }
        return robHelper(nums, 0);
    }

    public static int robHelper(int[] nums ,int i) {
        if (i >= nums.length) {
            return 0;
        }
        // 抢当前房屋
        int rob = nums[i] + robHelper(nums, i+2);
        // 不抢当前房屋
        int notRob = robHelper(nums, i+1);
        return Math.max(rob, notRob);
    }

    public static void main(String[] args) {
        System.out.println(rob(new int[]{1,2,3,1}));
        System.out.println(rob(new int[]{2,7,9,3,1}));
    }
}
